﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.Collections.Generic;
using DevExpress.XtraEditors;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Sys.Model;

namespace XCI.Sys.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class DepartmentTreeListLookUpEdit : TreeListLookUpEdit
    {
        private readonly AsyncWorker asyncWorker = new AsyncWorker();

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (DesignMode) return;

            this.Properties.ValueMember = "Id";
            this.Properties.DisplayMember = "Name";
            var treelist = this.Properties.TreeList;
            if (treelist != null)
            {
                treelist.Columns.AddRange(new[] {
                    new DevExpress.XtraTreeList.Columns.TreeListColumn
                    {
                        Caption = "名称",
                        FieldName = "Name",
                        Name = "treeColName",
                        Visible = true,
                        VisibleIndex = 0
                    }});
                treelist.KeyFieldName = "Id";
                treelist.ParentFieldName = "ParentId";
                treelist.OptionsSelection.EnableAppearanceFocusedCell = false;
                treelist.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
                treelist.OptionsView.ShowColumns = false;
                treelist.OptionsView.ShowHorzLines = false;
                treelist.OptionsView.ShowIndicator = false;
                treelist.OptionsView.ShowVertLines = false;
                treelist.OptionsBehavior.AutoPopulateColumns = true;
                var imageCollection1 = new DevExpress.Utils.ImageCollection();
                imageCollection1.InsertGalleryImage("documentmap_16x16.png", "images/navigation/documentmap_16x16.png", DevExpress.Images.ImageResourceCache.Default.GetImage("images/navigation/documentmap_16x16.png"), 0);
                imageCollection1.Images.SetKeyName(0, "documentmap_16x16.png");
                treelist.SelectImageList = imageCollection1;

                asyncWorker.RunCallback(e =>
                {
                    if (Properties.TreeList!=null)
                    {
                        e.Result = DeptService.Instance.SelectList("1");
                    }
                }).CompletCallback(e =>
                {
                    if (Properties.TreeList != null)
                    {
                        Properties.DataSource = e.Result;
                    }
                }).RunAsync();
            }
        }
    }
}